<?php

App::uses('AppModel', 'Model');

class Group extends AppModel {
    
    public $hasMany = array(
        'Product' => array( 'dependent' => true ));
    
    public function afterDelete() {
        $dirname = IMG_DIR . 'products' . DS . $this->id;
        if (is_dir($dirname)) {
            emptydir($dirname);
            rmdir($dirname);
        }
        
        return parent::afterDelete();
    }
    
    public function getMenu() {
        $groups = $this->find('all', array(
            'fields'     => array('Group.*'),
            'recursive'  => 0,
            'conditions' => array('Group.parent_id' => null)
        ));
        
        if (empty($groups)) {
            return array();
        } else {
            return Set::extract('{n}.Group', $groups);
        }
    }
    
    public function get($id, $fields='Group.*', $recursive=0) {
        return $this->find('first', array(
            'conditions' => array('Group.id' => $id),
            'recursive'  => $recursive,
            'fields'     => $fields
        ));
    }
    
}